import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import nattyStorage from 'natty-storage';
import DB from '../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    searchDivShow: false,
    searchData:[],
    pdata:[],
    domain:"",
    mediaUploadPath:""
  },

  onHandleChange:function(text){
    text=text.replace(/(^\s+)|(\s+$)/g, "")
    if(""!=text&&null!=text){
      let that=this;
      DB.WechatAPI.seachConstruction({
        pname:text
      }).then(
        function (content) {
          if(content.total>0){
            that.data.searchDivShow=update(that.data.searchDivShow, {$set: true});
            that.data.searchData=update(that.data.searchData, {$set: content.rows});
            that.updateComponent();
          }else{
            that.data.searchDivShow = update(that.data.searchDivShow, {$set: false});
            that.data.searchData = update(that.data.searchData, {$set: []});
            that.updateComponent();
          }

        },
        function (error) {
          console.log(error);
        }
      );
    }

  },

  onHandleCancel:function () {
    this.data.searchDivShow = update(this.data.searchDivShow, {$set: false});
    this.updateComponent();
  },

  onLoadData:function () {
    let that=this;
    DB.WechatAPI.loadLastSuggestionOneByType({page:0,size:30}).then(
      function (content) {
        if(content.totalCounts>0){
          that.data.pdata = update(that.data.pdata, {$set: content.dgzsList});
          that.data.domain=update(that.data.domain, {$set: content.domain});
          that.data.mediaUploadPath=update(that.data.mediaUploadPath, {$set: content.mediaUploadPath});
          that.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
